<template>
  <div class="content">
    <div class="title">
      收货地址
    </div>  
    <div class="shouhuobox">
      <div class="shouhuoitem" v-for="(item,index) of list" :key="index">
        <div class="itembox" v-if="index==0"  @click="addAddress(item)">
          <i class="el-icon-circle-plus tubiao"></i>
          <span>添加新地址</span>
        </div>
        <div class="itembox1" v-else>
          <div class="name">
            {{item.customerConsigneeName}}
          </div>
          <div class="dianhau">
            {{desensitizePhone(item.customerConsigneePhone)}}
          </div>
          <div class="dizhi">
            {{item.adressName+item.street}}
          </div>
          <div class="btns">
            <div class="btnitem" @click.stop="addAddress(item)">
              修改
            </div>
            <div class="btnitem" @click.stop="delAddress(item)">
              删除
            </div>
          </div>
        </div>
      </div>
    </div>  
    <editaddress ref="editaddress" :title="title" :open="open" :acitem="acitem" @submitForm="submitForm"  @cancel="cancel"  />
    
  </div>
</template>

<script>
import {addresslist,addressedit,addressdel} from '@/api/user'
import editaddress from './editaddress'
export default {
  name: "address2",//收货地址
  components:{
    editaddress
  },
  data() {
    const isMobile = (str) => {
      const reg = /^(1[3456789]\d{9})$/
      return reg.test(str)
    }
    return {
      list:[],
      acitem:null,
      open:false,
      form:{
        customerConsigneeName:'',
      },
      rules:{
        customerConsigneeName:[
          {required: true, message: '请输入名称', trigger: 'blur'},
        ],
        customerConsigneePhone:[
          {required: true, message: '请输入名称', trigger: 'blur'},
          {
            // 自定义验证函数
            validator: (rule, value, callback) => {
              // 返回true表示校验通过，返回false表示不通过
              return isMobile(value)
            },
            message: '手机号码不正确',
            // 触发器可以同时用blur和change
            trigger: ['blur'],
          }
        ],
      },
      props:{
        value: 'value',
        label: 'label',
        children: 'children',
      },
      addressoptions:[],
      values:[],
      title:'新增地址'
    };
  },
  mounted() {
      this.init()
  },
  methods: {
    delAddress(item){
      this.$confirm('此操作将永久删除该地址, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await addressdel({
          // customerId:this.$store.state.user.customerId,
          customerConsigneeId:item.customerConsigneeId
        })
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
        this.init()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    cancel(){
      this.open=false
    },
    handleChange(e){
        console.log(e)
    },
    async submitForm(e){
      console.log(e)
      this.init()
      this.open=false
      
    },
    addAddress(item){
      // console.log(item)
      this.acitem=null
     
      if(!item){
        this.title='新增收货地址'
      }else{
        this.acitem=item
        this.title='修改收货地址'
      }
      this.open=true
      
    },
    async init(){
      var customerId=this.$store.state.user.customerId
      const res =await addresslist({
        customerId:customerId
      })
      console.log(res)
      var arr=res.data
      arr.unshift({})
      this.list=arr

    },
    desensitizePhone(value) {
      if (!value) return '';
      return value.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
    }
  },
  
  
};
</script>
<style scoped lang="scss">
.content{
  padding: 16px;
  box-sizing: border-box;
  .title{
    font-weight: bold;
    font-size: 24px;
    color: #333333;
    line-height: 36px;
    text-align: left;
    margin-bottom: 16px;
  }
  .shouhuobox{
    display: flex;
    flex-wrap: wrap;
    gap: 16px 16px;
    .shouhuoitem{
      width: 286px;
      height: 170px;
      border-radius: 6px 6px 6px 6px;
      border: 1px solid #E0E0E0;
      cursor: pointer;
      position: relative;
      .itembox{
        width: 100%;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: column;
       
        .tubiao{
          font-size: 40px;
          color: #828282;
        }
        span{
          font-weight: 400;
          font-size: 14px;
          color: #828282;
          line-height: 20px;
          text-align: center;
          margin-top: 8px;
        }
      }
      .itembox1{
        padding: 16px;
        box-sizing: border-box;
       
        .name{
          font-weight: 400;
          font-size: 20px;
          color: #000000;
        }
        .dianhau{
          font-weight: 400;
          font-size: 14px;
          color: #828282;
          margin: 16px 0;
        }
        .dizhi{
          font-weight: 400;
          font-size: 14px;
          color: #828282;
          line-height: 20px;
          text-align: left;
        }
        .btns{
        position: absolute;
        bottom: 16px;
        right: 16px;
          display: none;
          align-items: center;
          justify-content: flex-end;
          .btnitem{
            font-weight: 400;
            font-size: 14px;
            color: #0672F9;
            line-height: 20px;
            text-align: left;
            cursor: pointer;
            margin-left: 16px;
          }
        }
      }
    }
    .shouhuoitem:hover,.shouhuoitem:active{
      border: 1px solid #0672F9;
      .btns{
        display: flex;
      }
    }
  }
}
</style>
